package com.myGisChannel.controller;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.myGis.entities.Layer;
import com.myGis.entities.Project;
import com.myGis.entities.User;
import com.myGis.services.IMyGisHelpersServices;
import com.myGis.services.IMyGisServices;
import com.myGis.services.MyGisHelpersServices;
import com.myGis.services.MyGisServer;
import com.myGisChannel.utils.BuildFormUtils;

/***
 * Controlador encargado de resolver la peticion "Agregar informacion Espacial"
 * @author Administrador
 *
 */
public class LoadLayerInfoController extends MultiActionController{
	
	static String LAYER_TO_WORK = "layerToWork"; 
	/**
	 * Retroma al viw la informacion asociada a una capa custon.
	 * @param request
	 * @param response
	 * @return En el MAV retorna 
	 * 	--> customLayer: Informacion de la capa, nombre, id etc.
	 * 	--> layerContent: Contiene un JSon con la informacion que contiene la capa custom.
	 * @throws Exception
	 */
	public ModelAndView loadLayerInfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mav = new ModelAndView("layersPages/layerLoadInfo");
		String idProject= request.getParameter("idProject");
		String idLayer= request.getParameter("idLayer");
		User usuario = (User) request.getSession(true).getAttribute("user");
		Project proyecto = getProyecto(usuario.getProjects(),idProject);
		
		mav.addObject("longitud", proyecto.getLongitudeInitial());
		mav.addObject("latitud", proyecto.getLatitudeInitial());
		mav.addObject("zoon", proyecto.getZoomInitial());

		IMyGisServices myGisServices = MyGisServer.getInstance();
		Layer layer = getLayer(proyecto.getLayers(), idLayer);

		request.getSession().setAttribute(LAYER_TO_WORK, layer);
		JSONArray jsonArrayCustom= new JSONArray();
		
		IMyGisHelpersServices imghs = new MyGisHelpersServices();
		JSONArray arrayJSon = imghs.layerCustomToJSon(layer);
		
		JSONObject data = new JSONObject();
			data.put("idLayerDB", layer.getLayerId());
			data.put("nameLayerkey", layer.getName());
			JSONArray jform = BuildFormUtils.getCustomForm(layer.getSimpleFields());
			data.put("layerForm", jform.toString());
			jsonArrayCustom.put(data);
		mav.addObject("customLayer",jsonArrayCustom);
		mav.addObject("layerContent",arrayJSon);
		mav.addObject("marker",layer.getMarker());
		
		return mav;
		
	}

	private Project getProyecto(List proyectos,String param) {
		Iterator<Project> iProyectos = proyectos.iterator();
		while (iProyectos.hasNext()) {
			Project proyecto = (Project) iProyectos.next();
			if(param.equals(proyecto.getProjectId().toString()))
				return proyecto;
			
		}
		return null;
	}	
	
	private Layer getLayer(List<Layer> layers, String idLayer){
		Iterator<Layer> iLayers = layers.iterator();
		while (iLayers.hasNext()) {
			Layer layer = (Layer) iLayers.next();
			if(idLayer.equals(layer.getLayerId().toString()))
				return layer;
			
		}
		return null;
	}

}
